﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AssertDataErrorInfo.cs" company="JDML Consulting Ltd.">
//   Copyright 2011 Photon Project - http://photon.codeplex.com
// </copyright>
// <summary>
//   Defines the AssertDataErrorInfo type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Photon.Testing
{
    using System;
    using System.ComponentModel;
    using System.Linq.Expressions;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Reflection;

    public static class AssertDataErrorInfo
    {
        public static void NotHasErrors<T>(T instance) where T : IDataErrorInfo
        {
            var errors = instance.Error;
            Assert.IsTrue(string.IsNullOrEmpty(instance.Error), "Expected instance of type {0} to not have errors but found {1}.",
                instance.GetType().Name, errors);
        }

        public static void HasErrors<T>(T instance, Expression<Func<object>> propertyExpression) where T : IDataErrorInfo
        {
            HasErrors(instance, propertyExpression.ExtractMemberName());
        }

        public static void HasErrors<T>(T instance, string propertyName) where T : IDataErrorInfo
        {
            Assert.IsFalse(string.IsNullOrEmpty(instance[propertyName]), "Expected property {0} to have errors but none were found.", propertyName);
        }

        public static void NotHasErrors<T>(T instance, Expression<Func<object>> propertyExpression) where T : IDataErrorInfo
        {
            NotHasErrors(instance, propertyExpression.ExtractMemberName());
        }

        public static void NotHasErrors<T>(T instance, string propertyName) where T : IDataErrorInfo
        {
            var errors = instance[propertyName];
            Assert.IsTrue(string.IsNullOrEmpty(errors), "Expected property {0} to not have errors but found {0}.", propertyName, errors);
        }
    }
}
